<template>
  <div class="workflow-detail">
    <div class="detail-header">
      <div class="header-left">
        <el-button link class="back-button" @click="router.back()">
          <el-icon><Back /></el-icon>
        </el-button>
        <h2 class="title">{{ workflow?.name || '工作流详情' }}</h2>
      </div>
      <div class="header-right">
        <el-button-group v-if="workflow">
          <el-button type="primary" @click="handleEdit">
            <el-icon><Edit /></el-icon>
            <span>编辑工作流</span>
          </el-button>
          <el-button type="primary" @click="handleExecute">
            <el-icon><VideoPlay /></el-icon>
            <span>执行工作流</span>
          </el-button>
        </el-button-group>
      </div>
    </div>

    <div class="content-wrapper" v-loading="loading">
      <template v-if="workflow">
        <div class="workflow-info">
          <el-descriptions :column="3" border>
            <el-descriptions-item label="创建者">{{ workflow.author }}</el-descriptions-item>
            <el-descriptions-item label="更新时间">
              {{ formatDateTime(workflow.updateTime) }}
            </el-descriptions-item>
            <el-descriptions-item label="标签">
              <el-tag
                v-for="label in workflow.label"
                :key="label"
                size="small"
                effect="plain"
                class="tag"
              >
                {{ label }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="描述" :span="3">
              {{ workflow.description }}
            </el-descriptions-item>
          </el-descriptions>
        </div>

        <div class="chapter-list">
          <el-timeline>
            <el-timeline-item
              v-for="(chapter, index) in workflow.chapters"
              :key="chapter.cid"
              :type="getTimelineItemType(index)"
              :hollow="true"
              :timestamp="`第 ${index + 1} 章节`"
            >
              <el-card class="chapter-card">
                <template #header>
                  <div class="chapter-header">
                    <h3 class="chapter-name">{{ chapter.name }}</h3>
                  </div>
                </template>
                <div class="chapter-content">
                  <div v-for="script in chapter.tags" :key="script.tid" class="script-item">
                    <div class="script-info">
                      <el-icon><Document /></el-icon>
                      <span class="script-name">{{ script.name }}</span>
                      <el-tag v-if="script.step" size="small" type="info" class="script-step">
                        {{ script.step }}
                      </el-tag>
                      <el-tag
                        v-if="script.status"
                        size="small"
                        effect="light"
                        :type="getStatusType(script.status)"
                      >
                        {{ script.status }}
                      </el-tag>
                      <el-tooltip
                        v-if="script.description"
                        :content="script.description"
                        placement="top"
                        effect="dark"
                      >
                        <el-icon class="info-icon"><InfoFilled /></el-icon>
                      </el-tooltip>
                    </div>
                    <div class="script-params" v-if="script.params && script.params.length > 0">
                      <div class="param-list">
                        <el-tag
                          v-for="param in script.params"
                          :key="param.name"
                          size="small"
                          effect="plain"
                          type="warning"
                        >
                          {{ param.name }}: {{ param.type }}
                        </el-tag>
                      </div>
                    </div>
                  </div>
                </div>

                <div class="chapter-content">
                  <div v-for="script in chapter.scripts" :key="script.tid" class="script-item">
                    <div class="script-info">
                      <el-icon><Document /></el-icon>
                      <span class="script-name">{{ script.name }}</span>
                      <el-tag v-if="script.step" size="small" type="info" class="script-step">
                        {{ script.step }}
                      </el-tag>
                      <el-tag
                        v-if="script.status"
                        size="small"
                        effect="light"
                        :type="getStatusType(script.status)"
                      >
                        {{ script.status }}
                      </el-tag>
                      <el-tooltip
                        v-if="script.description"
                        :content="script.description"
                        placement="top"
                        effect="dark"
                      >
                        <el-icon class="info-icon"><InfoFilled /></el-icon>
                      </el-tooltip>
                    </div>
                    <div class="script-params" v-if="script.params && script.params.length > 0">
                      <div class="param-list">
                        <el-tag
                          v-for="param in script.params"
                          :key="param.name"
                          size="small"
                          effect="plain"
                          type="warning"
                        >
                          {{ param.name }}: {{ param.type }}
                        </el-tag>
                      </div>
                    </div>
                  </div>
                </div>
              </el-card>
            </el-timeline-item>
          </el-timeline>
        </div>
      </template>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Edit, VideoPlay, Document, InfoFilled, Back } from '@element-plus/icons-vue'
import type { Workflow } from '@/api/types'
import { taskService } from '@/api/taskService'
import { formatDateTime } from '@/utils/date'

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const workflow = ref<Workflow>()

const loadWorkflow = async () => {
  loading.value = true
  try {
    const id = parseInt(route.params.id as string)
    workflow.value = await taskService.getWorkflowById(id)
  } catch (err) {
    console.error('获取工作流详情失败:', err)
    ElMessage.error('获取工作流详情失败')
  } finally {
    loading.value = false
  }
}

const getTimelineItemType = (index: number) => {
  const types = ['primary', 'success', 'warning', 'danger']
  return types[index % types.length]
}

const handleEdit = () => {
  if (!workflow.value) return
  router.push(`/task/workflow/edit/${workflow.value.id}`)
}

const handleExecute = () => {
  if (!workflow.value) return
  router.push({
    path: '/task/create',
    query: { workflow: workflow.value.id },
  })
}

const getStatusType = (status: string) => {
  const statusMap: Record<string, 'success' | 'warning' | 'danger' | 'info'> = {
    success: 'success',
    running: 'warning',
    failed: 'danger',
    pending: 'info',
  }
  return statusMap[status.toLowerCase()] || 'info'
}

onMounted(() => {
  loadWorkflow()
})
</script>

<style scoped lang="scss">
.workflow-detail {
  .detail-header {
    background-color: #fff;
    padding: 16px 24px;
    border-radius: 4px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
    margin-bottom: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .header-left {
      display: flex;
      align-items: center;
      gap: 8px;

      .back-button {
        padding: 8px;
        height: 32px;
        color: var(--el-text-color-regular);

        &:hover {
          color: var(--el-color-primary);
        }
      }

      .title {
        margin: 0;
        font-size: 20px;
        font-weight: 500;
        color: var(--el-text-color-primary);
        line-height: 32px;
      }
    }

    .header-right {
      :deep(.el-button) {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 4px;
        padding: 8px 16px;
        height: 32px;
        line-height: 1;

        .el-icon {
          margin: 0;
        }

        span {
          line-height: 1;
        }
      }
    }
  }

  .content-wrapper {
    padding: 24px;
    background-color: var(--el-bg-color);
    border-radius: 4px;
    min-height: calc(100% - 100px);

    .workflow-info {
      margin-bottom: 32px;

      .tag {
        margin-right: 4px;
      }
    }

    .chapter-list {
      :deep(.el-timeline) {
        padding-left: 16px;

        .el-timeline-item__node {
          left: -2px;
        }

        .el-timeline-item__wrapper {
          padding-left: 24px;
        }
      }

      .chapter-card {
        margin-bottom: 16px;

        :deep(.el-card__header) {
          padding: 12px 16px;
          border-bottom: 1px solid var(--el-border-color-lighter);
        }

        .chapter-header {
          display: flex;
          justify-content: space-between;
          align-items: center;

          .chapter-name {
            margin: 0;
            font-size: 16px;
            font-weight: 500;
            color: var(--el-text-color-primary);
          }
        }

        .chapter-content {
          padding: 16px;

          .script-item {
            padding: 12px;
            border-radius: 4px;
            background-color: var(--el-fill-color-light);
            box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
            position: relative;
            transition: all 0.3s ease;
            margin-bottom: 12px;

            &:last-child {
              margin-bottom: 0;
            }

            &:hover {
              background-color: var(--el-fill-color-lighter);
              transform: translateY(-1px);
              box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            }

            .script-info {
              display: flex;
              align-items: center;
              gap: 12px;

              .el-icon {
                font-size: 16px;
                color: var(--el-text-color-secondary);
              }

              .script-name {
                flex: 1;
                font-size: 14px;
                color: var(--el-text-color-primary);
                font-weight: 500;
              }

              :deep(.el-tag) {
                border-radius: 12px;
                height: 24px;
                line-height: 22px;
                padding: 0 10px;
              }

              :deep(.script-step) {
                background-color: var(--el-color-info-light-9);
                border-color: var(--el-color-info-light-7);
                color: var(--el-color-info);
              }

              .info-icon {
                color: var(--el-color-info);
                cursor: help;
                font-size: 14px;
                opacity: 0.7;
                transition: opacity 0.2s;

                &:hover {
                  opacity: 1;
                }
              }
            }

            .script-params {
              margin-top: 8px;
              margin-left: 32px;
              padding: 8px 12px;
              background-color: var(--el-bg-color);
              border-radius: 6px;
              border: 1px dashed var(--el-border-color);

              .param-list {
                display: flex;
                flex-wrap: wrap;
                gap: 4px;

                :deep(.el-tag) {
                  font-family: var(--el-font-family-monospace);
                  background-color: var(--el-color-warning-light-9);
                  border-color: var(--el-color-warning-light-7);
                  color: var(--el-color-warning-dark-2);
                  font-size: 12px;
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>
